﻿
using System;
namespace gbL.NetBoard.Helpers
{
    public class MessagesAdapter : IDisposable
    {
        private readonly IBlackboard _blackboard;

        private readonly ReceiverAdapter _receiver;

        private readonly SenderAdapter _sender;

        public IBlackboard Blackboard { get { return _blackboard; } }

        public ListenerMessageBehaviours MessageBehaviour
        {
            get { return _receiver.MessageBehaviour; }
            set { _receiver.MessageBehaviour = value; }
        }


        public MessagesAdapter(IBlackboard blackboard)
        {
            _blackboard = blackboard;
            _receiver = new ReceiverAdapter(blackboard);
            _sender = new SenderAdapter(blackboard);
        }


        public void EnableReceiver()
        {
            _receiver.Enabled = true;
        }

        public void DisableReceiver()
        {
            _receiver.Enabled = false;
        }


        public void Accept<T>(Action<T> messageAddedAction)
        {
            _receiver.Accept<T>(messageAddedAction, x => { });
        }

        public void Accept<T>(Action<T> messageAddedAction, Action<T> messageRemovedAction)
        {
            _receiver.Accept<T>(messageAddedAction, messageRemovedAction);
        }

        public void AcceptEntry<T>(Action<IBlackboardEntry> messageAddedAction)
        {
            _receiver.AcceptEntry<T>(messageAddedAction, x => { });
        }

        public void AcceptEntry<T>(Action<IBlackboardEntry> messageAddedAction, Action<IBlackboardEntry> messageRemovedAction)
        {
            _receiver.AcceptEntry<T>(messageAddedAction, messageRemovedAction);
        }


        public IBlackboardEntry SendEntry(object message)
        {
            return _sender.SendEntry(message);
        }

        public IBlackboardEntry SendEntry<T>(Action<T> initialise) where T : class
        {
            return _sender.SendEntry<T>(initialise);
        }

        public IBlackboardEntry SendEntry<T>() where T : class
        {
            return _sender.SendEntry<T>();
        }

        public T Send<T>(T message)
        {
            return (T)_sender.SendEntry(message).Message;
        }

        public T Send<T>(Action<T> initialise) where T : class
        {
            return (T)_sender.SendEntry<T>(initialise).Message;
        }

        public T Send<T>() where T : class
        {
            return (T)_sender.SendEntry<T>().Message;
        }


        public void Dispose()
        {
            _receiver.Dispose();
        }
    }
}
